<?php

function doUpgradeFx($upgrade_order)
{
    $fx_log = '../../attachment/upgradelog/';
    if (!file_exists($fx_log)) {
        mkdirs($fx_log);
    }
    $fx_log .= (date('Y-m-d') . '.txt');
    //判断此订单是否处理过分销
    if ($upgrade_order['fx_state']) {
        return true;
    }
    $child = pdo_get('yq_haircircle_users', array('id' => $upgrade_order['user_id']));

    if (empty($child) || $child['user_level'] < 1) {
        file_put_contents($fx_log, $upgrade_order['user_id'] . ':用户ID' . $upgrade_order['user_id'] . '不存在或用户未升级成功' . PHP_EOL, FILE_APPEND);
        return true;
    }

    if (!$child['pid']) {
        file_put_contents($fx_log, $upgrade_order['user_id'] . ':用户ID' . $upgrade_order['user_id'] . '没有上级，无需分销' . PHP_EOL, FILE_APPEND);
        return true;
    }


    // 分销配置
    $fenxiao_setting = cmf_get_option('fenxiao_setting_v1.0');
    if (!$fenxiao_setting['fenxiao_switch']) {
        return true;//没有开启分销
    }

    pdo_begin();
    //分销逻辑开始
    try {
        // 拥有上级 且上级是会员 执行分销
        $upUser = pdo_get('yq_haircircle_users', array('id' => $child['pid']), array('id', 'pid', 'mobile', 'user_money','user_level'));
        if (!empty($upUser) && $upUser['user_level'] == 1 && $fenxiao_setting['vip_upgrade_first_per'] > 0) {
            $fxyj = $fenxiao_setting['vip_upgrade_first_per'] * $fenxiao_setting['vip_upgrade_price'] / 100;
            $fxyj = number_format($fxyj,2);
            $res = pdo_update('yq_haircircle_users', array('user_money' => $upUser['user_money'] + $fxyj), array('id' => $upUser['id']));
            if (empty($res)) {
                pdo_rollback();
                file_put_contents($fx_log, $upgrade_order['user_id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，更新余额失败' . PHP_EOL, FILE_APPEND);
                return false;
            }
            //写入余额变更日志
            $mlog['oid'] = $upgrade_order['id'];
            $mlog['user_id'] = $upUser['id'];
            $mlog['type'] = 3;
            $mlog['change'] = $fxyj;
            $mlog['mobile'] = $upUser['mobile'];
            $mlog['balance'] = $upUser['user_money'] + $fxyj;
            $mlog['remark'] = "直推下级({$upgrade_order['user_id']})升级VIP返佣:{$fxyj}";
            $mlog['create_time'] = time();
            $res = pdo_insert('yq_haircircle_user_money_log',$mlog);
            if(empty($res)){
                pdo_rollback();
                file_put_contents($fx_log, $upgrade_order['user_id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，插入余额变更失败' . PHP_EOL, FILE_APPEND);
                return false;
            }
            //写入分销日志
            $sLog = [
                'oid' => $upgrade_order['id'],
                'yong_type' => 1,
                'user_id' => $upUser['id'],
                'sup_id' => $upgrade_order['user_id'],
                'fxprice' => $fenxiao_setting['vip_upgrade_price'],
                'fxyj' => $fxyj,
                'remark' => "直推下级({$upgrade_order['user_id']})升级VIP返佣:{$fxyj}",
                'create_time' => time()
            ];

            $res = pdo_insert('yq_haircircle_user_fx_log',$sLog);
            if(empty($res)){
                pdo_rollback();
                file_put_contents($fx_log, $upgrade_order['user_id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，写入分销日志失败' . PHP_EOL, FILE_APPEND);
                return false;
            }
        }
        $res = pdo_update('yq_haircircle_upgrade_order',array('fx_state'=>1),array('id'=>$upgrade_order['id']));
        if(empty($res)){
            pdo_rollback();
            file_put_contents($fx_log, $upgrade_order['id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，更新fx_state状态失败' . PHP_EOL, FILE_APPEND);
            return false;
        }
        //更改分销状态fx_state
        pdo_commit();
    } catch (\Exception $e) {
        pdo_rollback();
        file_put_contents($fx_log, $upgrade_order['id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，'.$e->getMessage() . PHP_EOL, FILE_APPEND);
        ajaxError('分销系统出错：' . $e->getMessage());
    }

    return true;
}